﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_ADDUOM : RibbonForm
    {
        public M_ADDUOM()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public DataTable LineItems { get; set; }

        void InitializaTable()
        {
            DataTable _table = new DataTable();

            _table.Columns.Add("RecIndex", typeof(int));
            _table.Columns.Add("ItemCode", typeof(string));
            _table.Columns.Add("ItemName", typeof(string));
            _table.Columns.Add("UoM", typeof(string));
            _table.Columns.Add("Dscription", typeof(string));
            _table.Columns.Add("BarCode", typeof(string));
            _table.Columns.Add("ConvQty", typeof(int));
            _table.Columns.Add("SmallestUoMConv", typeof(double));
            _table.Columns.Add("DefaultUoM", typeof(bool));


            this.LineItems = _table;
        }

        void BindGrid()
        {
            UoMManager oManager = new UoMManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable _table = oManager.GetUnitMeasures(txtItemCode.Text);

            oManager.Close();
            this.LineItems = _table;
            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void M_AddUoM_Load(object sender, EventArgs e)
        {
            InitializaTable();

            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UoMManager oManager = new UoMManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            oManager.UpdateMeasure(this.LineItems);
            oManager.Close();

            MessageBox.Show("Updated successfully!!!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddMeasure oForm = new AddMeasure();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ItemCode = txtItemCode.Text;
            oForm.ItemName = txtItemName.Text;

            oForm.ShowDialog();

            BindGrid();

        }

        private void txtItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_ITM_UOM oForm = new FND_ITM_UOM();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();
            
            txtItemCode.Text = oForm.SelectedItemCode;
            txtItemName.Text = oForm.SelectedItemDescription;
            BindGrid();
        }

        private void txtItemCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ItemManager oManager = new ItemManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                DataTable _table = oManager.GetItemMasterData(txtItemCode.Text);
                if (_table.Rows.Count > 0)
                {
                    DataRow oRow = _table.Rows[0];

                    bool IsInventory = (bool)oRow["IsInventoryItem"];

                    if (IsInventory)
                    {
                        txtItemCode.Text = oRow["ItemCode"].ToString();
                        txtItemName.Text = oRow["Description"].ToString();
                        BindGrid();
                    }
                }
                oManager.Close();
            }

            if (e.KeyCode == Keys.Tab)
            {
                ItemManager oManager = new ItemManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                DataTable _table = oManager.GetItemMasterData(txtItemCode.Text);

                if (_table.Rows.Count > 0)
                {
                    DataRow oRow = _table.Rows[0];

                    bool IsInventory = (bool)oRow["IsInventoryItem"];

                    if (IsInventory)
                    {
                        txtItemCode.Text = oRow["ItemCode"].ToString();
                        txtItemName.Text = oRow["ItemName"].ToString();
                        BindGrid();
                    }
                }
                oManager.Close();
            }
        }

        private void txtItemCode_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ItemManager oManager = new ItemManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                DataTable dtITEM = oManager.GetItemMasterData(txtItemCode.Text);

                if (dtITEM.Rows.Count > 0)
                {
                    DataRow oRow = dtITEM.Rows[0];


                    txtItemCode.Text = oRow["ItemCode"].ToString();
                    txtItemName.Text = oRow["Description"].ToString();

                    BindGrid();
                }

                oManager.Close();
            }

        }
    }
}
